//
//  AkuVisitorView.swift
//  AkuSwiftSina
//
//  Created by 阿酷 on 16/7/19.
//  Copyright © 2016年 AkuApp. All rights reserved.
//

import UIKit

class AkuVisitorView: UIView {

    /// 点击登入按钮的 回调
    var loginButtonClick: (()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        setViewSnapKit()
    }
    
    /// 登入/注册 按钮 的点击事件    //因为 swift 是 静态型 语言 (OC 是运行时)  所以 私有属性不会被调用 要 标记成@objc
    @objc private func didLoginButton() {
        
        loginButtonClick?()
    }
    
    /// 根据不同控制器 设置不同提示信息,图片
    func setPromptText(prompt: String?, imgName: String?) {
        
        if let promptText = prompt, let IMGName = imgName {
            
            houseImageView.image = UIImage(named: IMGName)
            promptLabel.text = promptText
            
            /// 可以移除 旋转的 view
            smalliconImageView.removeFromSuperview()
        }
    }
    
    /// 设置 子控件的 约束
    private func setViewSnapKit() {
        
        // 添加到 View 上
        self.addSubview(smalliconImageView)
        self.addSubview(maskImageView)
        self.addSubview(houseImageView)
        self.addSubview(promptLabel)
        self.addSubview(loginButton)
        self.addSubview(registerButton)
        
        smalliconImageView.sizeToFit()
        
        //MARK: - 设置约束
        //house
        houseImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        // smallicon
        smalliconImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        // mask_smallicon
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        // 提示 label
        promptLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.size.equalTo(CGSizeMake(230, 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(36)
        }
        // 登录 button
        loginButton.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(promptLabel)
            make.size.equalTo(CGSizeMake(100, 36))
            make.top.equalTo(promptLabel.snp_bottom).offset(16)
        }
        // 注册 button
        registerButton.snp_makeConstraints { (make) in
            
            make.leading.equalTo(promptLabel)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
    }
    
    
    // MARK: - 懒加载 控件
    /// 当前界面中心图片 house
    lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 圆形 visitordiscover_feed_image_smallicon
    lazy var smalliconImageView: UIImageView = {
    
        let imgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        /// 设置 imgView 的 旋转效果
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置toValue
        anim.toValue = M_PI * 2
        // 设置时间
        anim.duration = 10
        // 设置重复次数
        anim.repeatCount = MAXFLOAT
        // 当切换控制器或者程序退到后台 默认动画会停止
        anim.removedOnCompletion = false
        // 添加动画
        imgView.layer.addAnimation(anim, forKey: nil)
        
        return imgView
    }()
    
    /// 遮罩 visitordiscover_feed_mask_smallicon
    lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 提示信息 PromptLabel
    lazy var promptLabel: UILabel = {
       
        let label: UILabel = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.grayColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center

        return label
    }()
    /// 登入 button
    lazy var loginButton: UIButton = {
    
        let button: UIButton = UIButton()
    
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 添加 点击事件
        button.addTarget(self, action: "didLoginButton", forControlEvents: .TouchUpInside)
        
        return button
    }()
    /// 注册 button
    lazy var registerButton: UIButton = {
        
        let button: UIButton = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 添加 点击事件
        button.addTarget(self, action: "didLoginButton", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    
    
    
    
    
    
    
    
    
    /// 使用 xib/sb 会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
